use super::c::{MemAlloc, RawGetBuf};
use super::cpblock::CPBlock;
use super::gp::GP;
use super::usrtctx::Context;
use std::ffi::{c_void, CString};

pub struct RawMemory {
    mem: CPBlock,
}

impl RawMemory {
    pub fn new(ctx: &mut Context, name: CString) -> Self {
        Self {
            mem: ctx.bind(name),
        }
    }
    pub fn alloc_gp(&self, size: u64) -> Option<GP> {
        match self.mem.mem().handle() {
            Some(h) => unsafe {
                let mut gp = GP::default();
                MemAlloc(h, &mut gp, size);
                Some(gp)
            },
            None => None,
        }
    }
    pub fn ualloc(&self, gp: *mut GP, size: u64) -> Option<*const c_void> {
        match self.mem.mem().handle() {
            Some(h) => unsafe { Some(MemAlloc(h, gp, size)) },
            None => None,
        }
    }
}
